package com.ptmall.app.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.bean.AddressBean;
import com.ptmall.app.bean.CouponBean;
import com.ptmall.app.bean.PageData;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.adapter.CouponAdapter;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.PullableListView;
import com.ptmall.app.view.TitleBarView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CouponActivity extends BaseMvpActivity {


    TitleBarView titleBarView;
    PullableListView mListview;
    PullToRefreshLayout mRefreshView;
    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    CouponAdapter mCouponAdapter;
    int page = 1;
    List<CouponBean> mDataLists = new ArrayList<>();

    boolean is_select;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_list);
    }

    @Override
    public void initViews() {
        is_select = getIntent().getBooleanExtra("is_select",false);

        titleBarView = (TitleBarView) findViewById(R.id.titlebar);
        mListview = (PullableListView) findViewById(R.id.listview);
        mRefreshView = (PullToRefreshLayout) findViewById(R.id.refresh_view);

        titleBarView.setText("优惠卷");
        titleBarView.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        mRefreshView.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page++;
                getData();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mRefreshView.autoRefresh();
    }

    @Override
    public void initEvents() {
        mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(is_select){
                    if(mDataLists.get(position).is_ok.equals("2")){
                        showMsg(mDataLists.get(position).style_name+"满"+mDataLists.get(position).man_use+"可用");
                        return;
                    }
                    Intent intent = new Intent();
                    // 获取用户计算后的结果
                    intent.putExtra("name", mDataLists.get(position).money); //将计算的值回传回去
                    intent.putExtra("id", mDataLists.get(position).recordId); //将计算的值回传回去
                    setResult(2, intent);
                    finish(); //结束当前的activity的生命周期
                }
            }
        });
        mCouponAdapter = new CouponAdapter(getContext(),mDataLists);
        mListview.setAdapter(mCouponAdapter);
    }


    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("cartIds", getIntent().getStringExtra("carids") + "");
        apiDataRepository.getCouponList(map, new ApiNetResponse<PageData<CouponBean>>() {
            @Override
            public void onSuccess(PageData<CouponBean> datas) {
                if (null != mRefreshView) {
                    mRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                    mRefreshView.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if (page == 1) {
                    mDataLists.clear();
                    if (null != datas && datas.getData().size() > 0) {
                        mDataLists.addAll(datas.getData());
                    } else {
                    }
                } else {
                    if (null != datas && datas.getData().size() > 0) {
                        mDataLists.addAll(datas.getData());
                    } else {
                        showMsg(R.string.has_no_more);
                    }
                }
                mCouponAdapter.pushData(mDataLists);
            }
        });
    }
}
